import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken,setUser,getUser,removeUser } from '@/utils/auth'
import { Message, MessageBox } from 'element-ui'
const user = {
  state: {
    token: getToken(),
    roleId: '',
    avatar: '',
    username:'',
    password:'',
    id:''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ROLE: (state, roleId) => {
      state.roleId = roleId
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_PASSWORD: (state, password) => {
      state.password = password
    },
    SET_ID: (state, id) => {
      state.id = id
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {

          const res = response.data;
          if(res.data.error!=null){
            Message({
              message: res.data.error.text,
              type: 'error',
              duration: 5 * 1000
            })
            resolve()
          }else {
            console.log(res.data.token)
            setToken(res.data.token)
            // setUser(res.data.user)
            commit('SET_TOKEN', res.data.token)
            commit('SET_USERNAME', res.data.user.username)
            // commit('SET_ROLE', res.data.user.roleid)
            commit('SET_ID', res.data.user.id)
            console.log(res.data.user.password)
            commit('SET_PASSWORD', res.data.user.password)
            resolve()
          }
        }).catch(error => {
          console.log('发生错误！', error)
          reject(error)
        })

      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      console.log('获取用户信息')
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const res = response.data
          if (res.data.user.roleid !== '') { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLE', res.data.user.roleid)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_USERNAME', res.data.user.username)
          // commit('SET_AVATAR', data.yhnc)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLE', '')
          commit('SET_PASSWORD', '')
          commit('SET_ID', '')
          commit('SET_USERNAME', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
